package cn.doitedu.dp.controller;

import cn.doitedu.dp.pojo.RuleVo;
import cn.doitedu.dp.pojo.TableRuleVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Controller
public class OdsTableDqController {
    Connection connection;

    public OdsTableDqController() throws SQLException {
        connection = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit47", "root", "root");
    }


    @RequestMapping("/dq/ods_table_dq")
    @ResponseBody
    public String odsTableDq(@RequestBody TableRuleVo tableRuleVo) {

        String sql = "insert into ods_table_dq values(?,?,?,?,?)";

        try {
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, tableRuleVo.getTarget_table());
            pst.setString(2, tableRuleVo.getTarget_date());

            for (RuleVo rule : tableRuleVo.getRules()) {

                pst.setString(3, rule.getRule_name());
                pst.setString(4, rule.getTarget_field());
                pst.setDouble(5, rule.getCheck_result());

                pst.execute();

            }
        } catch (Exception e) {

            return "failed";
        }

        return "success";
    }


}
